<?php
	class Member_Model extends CI_Model {
		function __construct()
		{
			parent::__construct();	
		}
		
		function lists($p=1 , $limit=50 , $level=-1 , $keyword='')
		{
			$start = ($p-1)*$limit;
			
			$this->db->select('users.*,user_company_profile.name ,user_company_profile.email AS c_email , user_company_profile.id AS c_id');
			$this->db->from('users');
			$this->db->join('user_company_profile', 'users.id = user_company_profile.user_id');
			
			if($level!=-1){
				if($level==1){
					$this->db->where('users.gold_active', $level); 
					$this->db->where('users.gold_start <=', time()); 
					$this->db->where('users.gold_finish >=', time());
				}else{
					$this->db->where('users.gold_active', 0); 
					$this->db->or_where('users.gold_start >=', time()); 
					$this->db->or_where('users.gold_finish <=', time());
				}
			}
			
			if($keyword!=''){
				$this->db->or_like('users.username', $keyword);
				$this->db->or_like('users.firstname', $keyword);
				$this->db->or_like('users.lastname', $keyword);
				$this->db->or_like('users.email', $keyword);
				$this->db->or_like('user_company_profile.name', $keyword);
				$this->db->or_like('user_company_profile.email', $keyword);
			}
			
			$this->db->order_by("users.username", "asc");
			$this->db->limit($limit , $start);
			
			$query = $this->db->get();
			return $query->result();
		}
		
		function status($id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$query = $this->get_member_info($id);
				
				$data = array(
					//'active' => ABS($query[0]->active - 1) ,
					'confirm' => ABS($query[0]->confirm - 1),
					'modified' => time()
				);
				$this->db->where('id', $id);
				$this->db->update('users', $data);
			}
		}
		
		function approve($id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$query = $this->get_member_info($id);
				
				$data = array(
					'active' => ABS($query[0]->active - 1) ,
					//'confirm' => ABS($query[0]->confirm - 1),
					'modified' => time()
				);
				$this->db->where('id', $id);
				$this->db->update('users', $data);
			}
		}
		
		function get_member_info($id)
		{
			$this->db->where('id', $id);
			$this->db->from('users');
			$query = $this->db->get();
			return $query->result();
		}
		
		function getUserByEmail($id,$email)
		{
			$this->db->where('email', $email);
			$this->db->where('id !=', $id);
			$this->db->from('users');
			$query = $this->db->get();
			return $query->row_array();
		}
		
		function getUserByUsername($id,$username)
		{
			$this->db->where('username', $username);
			$this->db->where('id !=', $id);
			$this->db->from('users');
			$query = $this->db->get();
			return $query->row_array();
		}
		
		function update_supplier($member)
		{
			if($member['id'] != null) {
				$this->load->helper('security');
				$data = array(
					'role_id' => $member['role_id'] ,
					'gold_start' => $member['gold_start'] ,
					'gold_finish' => $member['gold_finish'] ,
					'gold_update' => $member['gold_update'] ,
					'gold_active' => $member['gold_active'] ,
					'gold_regis' => $member['gold_regis'] ,
					'firstname' => $member['firstname'],
					'lastname' => $member['lastname'],
					'email' => $member['email'],
					'username' => $member['username'],
					'tel' => $member['tel'],
					'modified' => $member['modified']
				);
				if($member['password']!=''){
					$data['password'] = md5($member['password']);
				}
				$this->db->where('id', $member['id']);
				$this->db->update('users', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function update_company($company)
		{
			if(isset($company['name'])) {
				//$this->load->helper('security');
				$data = array(
					'name' => $company['name'] ,
					'main_cat_id' => $company['main_cat_id'] ,
					'cat_id' => $company['cat_id'] ,
					'address' => $company['address'],
					'district' => $company['district'],
					'geo_id' => $company['geo_id'],
					'province' => $company['province'],
					'postcode' => $company['postcode'],
					'country' => $company['country'],
					'agent' => $company['agent'],
					'website' => $company['website'],
					'email' => $company['email'],
					'tel' => $company['tel'],
					'fax' => $company['fax'],
					'brand' => $company['brand'],
					'c_modified' => $company['c_modified']
				);
				
				$this->db->where('id', $company['id']);
				$this->db->update('user_company_profile', $data);
				//echo $this->db->last_query();
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function update_company_img($input)
		{
			if($input['id'] != null) {
				$data = array(
					'img' => $input['img'] ,
				);
				$this->db->where('id', $input['id']);
				$this->db->where('user_id', $input['user_id']);
				$this->db->update('user_company_profile', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function update_company_logo($input)
		{
			if($input['id'] != null) {
				$data = array(
					'logo' => $input['logo'] ,
				);
				$this->db->where('id', $input['id']);
				$this->db->where('user_id', $input['user_id']);
				$this->db->update('user_company_profile', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function delete($id)
		{
			if($id != 0 ) {
				$this->db->where('id', $id); 
				return $this->db->delete('users');
			}else{
				return 0;
			}
		}
		
		function delete_member_company($user_id)
		{
			if($user_id != 0 ) {
				$this->db->where('user_id', $user_id); 
				return $this->db->delete('user_company_profile');
			}else{
				return 0;
			}
		}
		
		function get_member_message()
		{
			$this->db->from('text_message');
			$this->db->where('id',2);
			$query = $this->db->get();
			$result = $query->result();
			return $result[0]->detail;
		}
		
		function update_member_message($str)
		{
			$data = array(
				'detail' => $str ,
			);
			$this->db->where('id', 2);
			$this->db->update('text_message', $data);
			return $this->db->affected_rows();
		}
	}


/* End of file member_model.php */
/* Location: ./application/models/admin/member_model.php */